package data.promotion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;

import po.CouponPO;
import po.DiscountCouponPO;
import po.EquivalentCouponPO;
import po.ResultMessage;
import service.PromotionDataService;

public class PromotionData extends UnicastRemoteObject implements
		PromotionDataService {
	private static final long serialVersionUID = 1L;
	private float ratio;
	private float quota;
	List<EquivalentCouponPO> equivalentCoupons;
	List<DiscountCouponPO> discountCoupons;
	private ObjectInputStream inputStream;
	private ObjectOutputStream outputStream;

	public PromotionData() throws RemoteException {
		try {
			initData();
			openOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	private void initData() throws IOException {
		if (new File("data/promotion").exists()) {
			FileInputStream fis = new FileInputStream("data/promotion");
			inputStream = new ObjectInputStream(fis);

			while (fis.available() > 0) {
				try {
					equivalentCoupons = (List<EquivalentCouponPO>) inputStream
							.readObject();
					discountCoupons = (List<DiscountCouponPO>) inputStream
							.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
			inputStream.close();
		}
	}

	private void openOutputStream() throws IOException {
		FileOutputStream fos = new FileOutputStream("data/promotion");
		outputStream = new ObjectOutputStream(fos);
		outputStream.writeObject(equivalentCoupons);
		outputStream.writeObject(discountCoupons);
	}

	public synchronized void save() throws IOException {
		close();
		openOutputStream();
	}

	public synchronized void close() {
		try {
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public float getRatio() throws RemoteException {
		return ratio;
	}

	@Override
	public ResultMessage setRatio(float newRatio) throws RemoteException {
		ratio = newRatio;
		return ResultMessage.SUCCEED;
	}

	@Override
	public float getQuota() throws RemoteException {
		return quota;
	}

	@Override
	public ResultMessage setQuota(float newLimit) throws RemoteException {
		quota = newLimit;
		return ResultMessage.SUCCEED;
	}

	@Override
	public List<EquivalentCouponPO> getEquivalentCoupons()
			throws RemoteException {
		return equivalentCoupons;
	}

	@Override
	public ResultMessage addDiscountCoupon(DiscountCouponPO po)
			throws RemoteException {
		discountCoupons.add(po);
		return ResultMessage.SUCCEED;
	}

	@Override
	public List<DiscountCouponPO> getDiscountCoupons() throws RemoteException {
		return discountCoupons;
	}

	@Override
	public ResultMessage addEquivolentCoupon(EquivalentCouponPO po)
			throws RemoteException {
		equivalentCoupons.add(po);
		return ResultMessage.SUCCEED;
	}

	@Override
	public ResultMessage remove(CouponPO coupon) throws RemoteException {
		if (coupon.isEqu()) {
			for(CouponPO c : equivalentCoupons) {
				if(c.getValue() == coupon.getValue()) {
					equivalentCoupons.remove(c);
					return ResultMessage.SUCCEED;
				}
			}
		} else {
			for(CouponPO c : discountCoupons) {
				if(c.getValue() == coupon.getValue()) {
					discountCoupons.remove(c);
					return ResultMessage.SUCCEED;
				}
			}
		}
		System.out.println("No such coupon" + coupon.getValue());
		return ResultMessage.REJECTED;
	}
}